<?php

namespace common\models\demand;

use Yii;
use common\helpers\Url;
use common\helpers\ArrayHelper;
use common\models\user\UserFavorite;

/**
 * This is the model class for table "{{%demand}}".
 *
 * @property int $id
 * @property string $title
 * @property string|null $thumb
 * @property string|null $description
 * @property int $user_id
 * @property int $category_id
 * @property int $recommend_at
 * @property int $status
 * @property int $created_at
 * @property int $updated_at
 * @property int $deleted_at
 */
class Demand extends \common\db\ActiveRecord {

    use \common\traits\relation\User;

    /**
     * 状态
     */
    const STATUS_ACTIVE = 10;
    const STATUS_DEFAULT = 0;

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%demand}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['user_id', 'category_id', 'recommend_at', 'status', 'created_at', 'updated_at', 'deleted_at'], 'integer'],
            [['title'], 'required'],
            [['title', 'thumb', 'description'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'title' => 'Title',
            'thumb' => 'Thumb',
            'description' => 'Description',
            'user_id' => 'user_id',
            'category_id' => 'Channel ID',
            'recommend_at' => 'Recommend At',
            'status' => 'Status',
            'created_at' => 'Created At',
            'updated_at' => 'Updated At',
            'deleted_at' => 'Deleted At',
        ];
    }

    public function getCategory() {
        return $this->hasOne(DemandCategory::class, ['id' => 'category_id']);
    }

    public function getStats() {
        return $this->hasOne(DemandStats::class, ['demand_id' => 'id']);
    }

    public function getContent() {
        return $this->hasOne(DemandContent::class, ['demand_id' => 'id']);
    }

    public function getImages() {
        return $this->hasMany(DemandAttach::class, ['demand_id' => 'id'])->andWhere([
            'format' => DemandAttach::FORMAT_IMAGE,
            'solution_id' => 0,
        ]);
    }

    public function getSolutions() {
        return $this->hasMany(DemandSolution::class, ['demand_id' => 'id'])->active();
    }

    public function getMedias() {
        return $this->getImages();
    }

    public function getImageCount() {
        return $this->getImages()->count();
    }

    public function getFormat() {
        return 'default';
        if ($this->hasMethod('getMedias')) {
            $images = count($this->medias);
        } else {
            $images = $this->imageCount;
        }
        if ($images > 3) {
            return 'grid';
        } elseif ($images > 1) {
            return 'fence';
        } else {
            return 'default';
        }
    }

    public function getAttach() {
        return $this->hasOne(DemandAttach::class, ['demand_id' => 'id'])->andWhere([
            'format' => DemandAttach::FORMAT_FILE
        ]);
    }

    /**
     * 检测获取用户是否点赞过
     */
    public function getLiked() {
        if (Yii::$app->user->isGuest) {
            return false;
        }
        return $this->hasOne(CmsArticleLike::class, ['demand_id' => 'id'])->andWhere([
            'comment_id' => 0,
            'user_id' => Yii::$app->user->id
        ])->exists();
    }

    /**
     * 检测获取用户是否收藏过
     */
    public function getCollected() {
        if (Yii::$app->user->isGuest) {
            return false;
        }
        return $this->hasOne(UserFavorite::class, ['model_id' => 'id'])->andWhere([
            'model' => UserFavorite::MODEL_ARTICLE,
            'user_id' => Yii::$app->user->id
        ])->exists();
    }

    public function getBody() {
        if ($this->content) {
            return $this->content->bodyParagraph;
        }
        return [];
    }

    public function getThumbUrl() {
        $thumb = $this->thumb ?: ArrayHelper::getValue(Yii::$app->params, ['defaultImage', 'demandCover'], '/assets/demand/cover.png');
        return Url::attach($thumb);
    }

    public function getPublishTime() {
        return date('Y-m-d', $this->created_at);
//        if ($this->created_at <= 0) {
//            return '很久以前';
//        }
//        $duration = time() - $this->created_at;
//        if ($duration > 7 * 86400) {
//            $year = date('Y', $this->created_at);
//            if ($year - date('Y') != 0) {
//                return date('Y-m-d', $this->created_at);
//            }
//            return date('m-d H:i', $this->created_at);
//        } else {
//            return Yii::$app->formatter->asRelativeTime($this->created_at);
//        }
    }

    public function getMplink() {
        return [
            'type' => 'push',
            'route' => Url::to(['/pages/demand/main', 'id' => $this->id]),
        ];
    }

}
